package org.PropertyManageMentSystem.Manager;

import org.PropertyManageMentSystem.Data.*;

import java.sql.*;
import java.util.ArrayList;


public class ActionManager {
    private Connection conn;

    public ActionManager(MysqlConfig config) {
        String url = config.getUrl();
        String user = config.getUser();
        String password = config.getPassword();

        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            this.conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addAction(Long scheduleId, Long workerid, Timestamp starttime, Timestamp endtime, String info) {
        try {
            String sql = "Insert into action (scheduleId, workerId, starttime, endtime, info) values(?, ?, ?, ?, ?)";
            PreparedStatement stat = conn.prepareStatement(sql);//SQL语句已预编译并存储在PreparedStatement对象中。


            //setString(int parameterIndex, String x) 将指定的参数设置为给定的Java String值
            stat.setLong(1, scheduleId); //即将p.getName()所获得值赋给第1个问号的地方

            stat.setLong(2, workerid);//同理将getPrice()所获得的值赋给第2个问号地方
            System.out.println(starttime);
            System.out.println(endtime);
            stat.setTimestamp(3, starttime);
            stat.setTimestamp(4, endtime);
            stat.setString(5, info);

            //执行sql语句
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public ArrayList<Action> getActionByWorkerId(Long workerId){
        ArrayList<Action> actions = new ArrayList<Action>();
        try {
            String sql = "select * from action where workerId=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            stat.setLong(1, workerId);
            //因为executeQuery会有ResultSet的返回值
            ResultSet res = stat.executeQuery();

            //将res中的值取出给到集合里面
            while (res.next()) {
                Long actionId = res.getLong("actionId");
                Long scheduleId = res.getLong("scheduleId");
                Timestamp starttime = res.getTimestamp("starttime");
                Timestamp endtime = res.getTimestamp("endtime");
                String info = res.getString("info");

                Action action = new Action(actionId, scheduleId, workerId, starttime, endtime, info);
                //添加到容器
                actions.add(action);
            }
            return actions;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<Action> getActionByScheduleId(Long scheduleId){
        ArrayList<Action> actions = new ArrayList<Action>();
        try {
            String sql = "select * from action where scheduleId=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            stat.setLong(1, scheduleId);
            //因为executeQuery会有ResultSet的返回值
            ResultSet res = stat.executeQuery();

            //将res中的值取出给到集合里面
            while (res.next()) {
                Long actionId = res.getLong("actionId");
                Long workerId = res.getLong("workerId");
                Timestamp starttime = res.getTimestamp("starttime");
                Timestamp endtime = res.getTimestamp("endtime");
                String info = res.getString("info");

                Action action = new Action(actionId, scheduleId, workerId, starttime, endtime, info);
                //添加到容器
                actions.add(action);
            }
            return actions;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
